<template>
  <view class="home">
    <!-- #ifdef MP-WEIXIN || MP-QQ -->
    <!-- #endif -->
    <!-- 首页 -->
    <Index ref="index" v-if="currentTab.index === 0"></Index>
    <!-- 用户中心 -->
    <UserCenter ref="usercenter" v-if="currentTab.index === 4" @switch="switchTab"></UserCenter>
    <!-- 底部tabbar -->
    <!-- <Tabbar :currentIndex="currentTab.index" @switch="switchTab"/> -->
    <Tabbar :currentTab="0" :list="tabbarList"/>
    <ScrollToTop :scrollTop="scrollTop"/>
  </view>
</template>

<script>
  import Index from '@/pages/index/index'
  import UserCenter from '@/pages/usercenter/usercenter'
  import Tabbar from '@/pages/pagecomponents/base/tabbar.vue';
  import ScrollToTop from '@/pages/pagecomponents/other/scrolltotop'
  export default {
    components: {
      Index,
      UserCenter,
      Tabbar,
      ScrollToTop,
    },
    data() {
      return {
        scrollTop: 0, // 页面滚动高度
        tabbarList: ['index', '12348', 'mine'],
        currentTab: {
          index: 0,
          ref: 'index',
        },
      }
    },
    computed:{
      isDev(){
        return this.$store.state.isDev
      },
      userInfo(){
        return this.$store.state.userInfo
      },
    },
    onPageScroll(e) {
      // 滚动条滚动
      this.scrollTop = e.scrollTop
    },
    onPullDownRefresh(){
      // 下拉刷新
      this.$refs[this.currentTab.ref].refresh()
      uni.stopPullDownRefresh();
      // 刷新isdev
      // this.$store.commit('setIsDev')
      // 调用刷新用户信息方法
      this.$store.commit('refreshUserInfo')
    },
    onReachBottom() {
      // 触底函数
      this.$refs[this.currentTab.ref].nextPaging()
    },
    onShareAppMessage(res) {
      // 小程序分享
      return {
        title: '您的好友呼唤您一起享优惠啦',
        desc: `${this.appNameCNY}是最好的比价平台，一定要感谢你的朋友`,
        path: `/pages/home/index?index=0&&ref=index`,
      }
    },
    onShow(){
      // 注册当前页面refs到store
      // this.$store.commit('resignRefs', this.$refs)
    },
    onLoad(option) {
      if (option.redirect){ // 分享的是商品详情页
        uni.navigateTo({
          url: decodeURIComponent(option.redirect)
        });
      }else if(option.index && option.ref){
        this.currentTab = {
          index: Number(option.index),
          ref: option.ref,
        }
      }
    },
    methods: {
      switchTab(item){
        this.currentTab = item
      },
    }
  }
</script>

<style scoped lang="scss">
  .home {
    // padding-bottom: 80px;
  }
</style>
